package com.bhs.sansonglogistics.ui.adapter;

import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.OrderGoodsBean;
import com.bhs.sansonglogistics.dialog.MyClickListener;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * 货源
 */
public class OrderGoodsAdapter extends BaseQuickAdapter<OrderGoodsBean, BaseViewHolder> {


    //1普通  2选择订单 3上游来货 4报价记录 5拒绝记录 6在途资金
    private final int type;

    public OrderGoodsAdapter(int type) {
        super(R.layout.item_order_goods);
        this.type = type;
    }

    private MyClickListener listener;

    @Override
    protected void convert(@NonNull BaseViewHolder helper, OrderGoodsBean item) {
        helper.addOnClickListener(R.id.tv_order_number);
        helper.addOnClickListener(R.id.btn_confirm_receipt);
        helper.addOnClickListener(R.id.btn_incremental_expenses);
        helper.addOnClickListener(R.id.btn_edit);
        helper.addOnClickListener(R.id.btn_electronic_order);
        helper.addOnClickListener(R.id.btn_confirm_departure);
        helper.addOnClickListener(R.id.btn_confirm_sign_for);
        helper.addOnClickListener(R.id.btn_receipt_pending);
        helper.addOnClickListener(R.id.btn_check_offer);
        helper.addOnClickListener(R.id.btn_delete_order);
        helper.addOnClickListener(R.id.btn_check_order, R.id.tv_claim_goods);
        helper.addOnClickListener(R.id.btn_confirm_cancel, R.id.btn_claim_goods);
        helper.addOnClickListener(R.id.btn_waybill, R.id.btn_view_cancel);
        helper.addOnClickListener(R.id.btn_cash_register, R.id.tv_sign_for, R.id.tv_receipt);
        helper.setText(R.id.tv_order_number, String.format("订单号:%s", item.getDeliver_sn()));
        helper.setText(R.id.tv_status, item.getStatus_msg());
        helper.setGone(R.id.btn_appoint, item.getOrder_type() == 1);//1指派 2极速
        helper.setGone(R.id.btn_top_speed, item.getOrder_type() == 2);
        helper.setGone(R.id.btn_make_appointment, item.getIs_yuyue() > 0);
        helper.setText(R.id.tv_place_departure, item.getPickup_city());
        helper.setText(R.id.tv_destination, item.getReceive_city());
        helper.setText(R.id.tv_departure_address, item.getPickup_address());
        helper.setText(R.id.tv_delivery_type, MyUtils.getDeliveryType(item.getJiaohuo_type()));
        helper.setText(R.id.tv_arrival_address, item.getReceive_address());
        helper.setText(R.id.tv_distribution_type, MyUtils.getDistributionType(item.getPeisong_type()));
        helper.setText(R.id.tv_haulage_time, MyUtils.getHaulageTime(item.getDeliver_type(), item.getTransport_day_max(), item.getTransport_day_min()));
        helper.setText(R.id.tv_cargo_time, String.format("%s/%s件/%s吨/%s立方", item.getGoods_name(), item.getGoods_number(),
                MyUtils.checkPoint(item.getGoods_weight()), MyUtils.checkPoint(item.getGoods_volume())));
        helper.setText(R.id.tv_added_services, String.format("%s%s", MyUtils.getReceiptType(item.getReceipt_type()), MyUtils.isInvoice(item.getIs_invoice())));
        if (item.getIs_yuyue() > 0 && item.getStatus() == 5) {
            helper.setText(R.id.tv_order_time, String.format("预约时间：%s", DateUtils.descriptiveData(item.getYuyue_time())));
        } else {
            helper.setText(R.id.tv_order_time, String.format("下单时间：%s", DateUtils.descriptiveData(item.getCreate_time())));
        }
        helper.setText(R.id.tv_order_price, String.format("￥%s", MyUtils.getPayStatus(item.getPay_to_logistics_money(), item.getNo_pay_total(), item.getPay_status(), item.getPay_method())));
        helper.setGone(R.id.btn_confirm_receipt, item.getStatus() == 5);
        helper.setGone(R.id.btn_claim_goods, item.getStatus() == 5 && item.getJiaohuo_type() == 2);
        helper.setGone(R.id.btn_confirm_departure, item.getStatus() == 7 && item.getCancel_status() == 0);
        helper.setGone(R.id.btn_waybill, item.getStatus() == 9);
        helper.setGone(R.id.btn_confirm_sign_for, item.getStatus() == 11);
        helper.setGone(R.id.tv_sign_for, item.getStatus() == 11);
        helper.setGone(R.id.btn_receipt_pending, item.getStatus() == 13);
        helper.setGone(R.id.tv_receipt, item.getStatus() == 13);
        helper.setGone(R.id.btn_view_cancel, item.getStatus() == 99);
        helper.setGone(R.id.btn_incremental_expenses, item.getStatus() < 15);
        helper.setGone(R.id.btn_confirm_cancel, item.getCancel_status() == 1);
        helper.setGone(R.id.ll_operation, type == 1);
        helper.setGone(R.id.cb_select, type == 2);
        helper.setGone(R.id.ll_upstream, type == 3);
        helper.setGone(R.id.ll_history, type == 4 || type == 5);
        helper.setGone(R.id.btn_check_offer, type == 4);
        helper.setGone(R.id.tv_status, type != 4);
        // helper.setGone(R.id.btn_cash_register, item.getPay_status() == 0 && item.getStatus() != 99);
        CheckBox checkBox = helper.getView(R.id.cb_select);
        checkBox.setChecked(item.isChecked());
        helper.setText(R.id.tv_self_pickup_code, String.format("取货码：%s", item.getSelf_pickup_code()));
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (item.getCancel_status() == 0) {
                    item.setChecked(b);
                    listener.onClick(null);
                } else {
                    checkBox.setChecked(false);
                    ToastUtil.show("此订单处于待取消状态无法选中");
                }
            }
        });
        helper.setGone(R.id.ll_freight, type == 6);
        helper.setGone(R.id.tv_order_price, type != 6);
        Spanned html = Html.fromHtml("费用：<font color=\"#FF6067\">" + item.getPay_to_logistics_money() + "</font>" + (item.getPay_status() == 2 ? "(已支付)" : "(待支付)"));
        helper.setText(R.id.tv_freight, html);
    }

    public void setMyClickListener(@Nullable MyClickListener l) {
        this.listener = l;
    }

}
